package wdc.test;



import java.util.ArrayList;

import  fbp.engine.Component;
import  fbp.engine.ComponentDescription;
import  fbp.engine.InPort;
import  fbp.engine.InputPort;
import  fbp.engine.OutPort;
import  fbp.engine.OutputPort;
import  fbp.engine.Packet;


/**
 * Component to copy all incoming packets - mostly used for debugging purposes.
 */
@ComponentDescription("Evaluate MAX value from serie")
@OutPort("OUT")
@InPort("IN")
public class MAX extends Component {

//  static final String copyright = "Copyright 2007, 2012, J. Paul Morrison.  At your option, you may copy, "
//      + "distribute, or make derivative works under the terms of the Clarified Artistic License, "
//      + "based on the Everything Development Company's Artistic License.  A document describing "
//      + "this License may be found at http://www.jpaulmorrison.com/fbp/artistic2.htm. "
//      + "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";

  InputPort inport;
  OutputPort outport;
  double maxValue = Double.NaN;

  @Override
  protected void execute() {
    Packet p = inport.receive();
    if (p==null)return;
    
    for(; p!=null; p = inport.receive()){
	    double current = ((Double)p.getContent()).doubleValue();
	    if(Double.isNaN(maxValue)){
	    	maxValue = current;
	    }else{
	    	maxValue = (maxValue>current)? maxValue : current;
	    }
	    drop(p);
    }
   
    outport.send(create(new Double(maxValue)));
   
  }

  @Override
  protected void openPorts() {
    inport = openInput("IN");
    outport = openOutput("OUT");
  }
}
